package com.invengo.testrfid.app.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

import com.invengo.testrfid.MyApplication;

public class WifiStatueUtils {

    /**
     * 没有网络
     */
    public static int DISDONNECTED = 0;

    /**
     * WIFI连接
     */
    public static int WIFI_CONNECTED = 2;

    /**
     * 手机网络连接
     */
    public static int MOBILE_CONNECTED = 1;

    private static ConnectivityManager manager;

    /**
     * 检测网络状态
     * <p>
     * 0没有网络，1手机网络，2wife网络
     *
     * @param context
     * @return
     */
    public static int getNetWorkStatue(Context context) {

        if (checkNetworkState(context)) {
            if (isWife()) {
                return 2;
            } else {
                return 1;
            }
        } else {
            return 0;
        }
    }

    /**
     * 检测网络是否连接
     *
     * @return
     */
    public static boolean checkNetworkState(Context context) {
        boolean flag = false;
        // 得到网络连接信息
        manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        // 去进行判断网络是否连接
        if (manager.getActiveNetworkInfo() != null) {
            flag = manager.getActiveNetworkInfo().isAvailable();
        }
        return flag;
    }

    /**
     * 判断是否是wife连接
     */
    public static boolean isWife() {

        manager = (ConnectivityManager) MyApplication.getContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                .getState();
        if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isNetworkAvailable(Context context) {
        if (null == context) {
            return false;
        }
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo = manager.getActiveNetworkInfo();
        if (networkinfo != null && networkinfo.isConnected()) {
            return true;
        }
        return false;
    }

}
